package net.atomarrow.util;

import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 * @author Michael
 *
 */
public class StringUtil {

	/**
	 * 从数组字符串中去掉某个值，
	 * 例如从1，2，3，5中去掉3
	 * @return
	 */
	public static String delFromArrayString(String arrayString, String delValue) {
		if (isBlank(arrayString) ) {
			return arrayString;
		}
		if (arrayString.startsWith(",") == false) {
			arrayString = "," + arrayString;
		}
		if (arrayString.endsWith(",") == false) {
			arrayString += ",";
		}
		arrayString = arrayString.replace("," + delValue + ",", ",");
		return arrayString.equals(",") ? "" : arrayString.substring(1, arrayString.length() - 1);
	}


	public static boolean isNumeric(String str){
		  if(isBlank(str)){return false;}
		   Pattern pattern = Pattern.compile("[0-9]*");
		   Matcher isNum = pattern.matcher(str);
		return isNum.matches();
	}
    /**
     * 首字母小写
     * @param s
     * @return
     */
    public static String toLowerCaseFirstWord(String s) {
        if(isBlank(s)){
            return s;
        }
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        }
        if(s.length()==1){
            return Character.toLowerCase(s.charAt(0))+"";
        }
        return Character.toLowerCase(s.charAt(0))+s.substring(1);
    }
    /**
     * 首字母大写
     * @param s
     * @return
     */
    public static String toUpperCaseFirstWord(String s) {
        if(isBlank(s)){
            return s;
        }
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        }
        if(s.length()==1){
            return Character.toUpperCase(s.charAt(0))+"";
        }
        return Character.toUpperCase(s.charAt(0))+s.substring(1);
    }
    /**
     * 判断空
     * @param s
     * @return
     */
    public static boolean isBlank(String s){
		return s == null || s.trim().equals("");
	}
    /**
     * 判断不是空
     * @param s
     * @return
     */
    public static boolean isNotBlank(String s){
        return !isBlank(s);
    }
    /**
     * 判断空
     * @param s
     * @return
     */
    public static boolean isEmpty(String s){
		return s == null || s.equals("");
	}
    /**
     * 判断不是空
     * @param s
     * @return
     */
    public static boolean isNotEmpty(String s){
        return !isEmpty(s);
    }
    /**
     * 判断存在多少个指定字符
     * @param s
     * @param c
     * @return
     */
    public static int count(String s,char c){
    	if(s==null){
    		return 0 ;
    	}
    	int count=0;
		for(int i=0;i<s.length();i++)
		{
			if(c==s.charAt(i)){
				count++;
			}
		}
		return count;
    }
    /**
     * 数组转字符串
     * @param arrays
     * @return
     */
    public static String toString(String[] arrays){
    	if(arrays==null){
    		return null;
    	}
    	StringBuilder sb = new StringBuilder();
    	for(String array:arrays){
    		sb.append(array);
    		sb.append(",");
    	}
    	sb.deleteCharAt(sb.length()-1);
    	return sb.toString();
    }
    /**
     * 替换
     * @param str
     * @param from
     * @param to
     * @return
     */
    public static String replace(String str,String from,String to){
        if(str==null){
            return null;
        }
        return str.replaceAll(from, to);
    }

    /**
     * list转字符串
     * @param list
     * @param separator
     * @return
     */
	public static String listToString(List list, String separator) {
		if(list==null||list.isEmpty()){
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i)==null){
				continue;
			}
			sb.append(list.get(i)).append(separator);
		}
		return sb.substring(0, sb.toString().length() - 1);
	}
    /**
     * array转字符串
     * @param list
     * @param separator
     * @return
     */
	public static <T extends Object> String arrayToString(T[] args, String separator) {
		if(args==null||args.length==0){
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < args.length; i++) {
			if(args[i]==null){
				continue;
			}
			sb.append(args[i]).append(separator);
		}
		return sb.substring(0, sb.toString().length() - 1);
	}
	/**
	 * 获得字符串的前多少字
	 * @return
	 */
	public static String getPreWords(String str,int count,String omittedWords){
		String newStr;
		if(str==null){
			return null;
		}
		if(str.length()<=count){
			return str;
		}
		newStr=str.substring(0,count);
		if(omittedWords!=null){
			return newStr+omittedWords;
		}
		return newStr;
	}

	/**
	 * 对字符串编码
	 * @param src
	 * @return
	 */
	public static String escape(String src) {
		int i;
		char j;
		StringBuilder tmp = new StringBuilder();
		tmp.ensureCapacity(src.length() * 6);
		for (i = 0; i < src.length(); i++) {
			j = src.charAt(i);
			if (Character.isDigit(j) || Character.isLowerCase(j)
					|| Character.isUpperCase(j))
				tmp.append(j);
			else if (j < 256) {
				tmp.append("%");
				if (j < 16)
					tmp.append("0");
				tmp.append(Integer.toString(j, 16));
			} else {
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

	/**
	 * 对字符串解码
	 * @param src
	 * @return
	 */
	public static String unescape(String src) {
		StringBuilder tmp = new StringBuilder();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(src
							.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(src
							.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src, lastPos, pos);
					lastPos = pos;
				}
			}
		}
		return isoToUTF(tmp.toString());
	}
	/**
	 * @disc 对字符串重新编码gbk
	 * @param src
	 * @return
	 */
	public static String isoToGB(String src) {
		String strRet = null;
		try {
			strRet = new String(src.getBytes("ISO_8859_1"), "GB2312");
		} catch (Exception e) {

		}
		return strRet;
	}

	/**
	 * @disc 对字符串重新编码utf8
	 * @param src
	 * @return
	 */
	public static String isoToUTF(String src) {
		String strRet = null;
		try {
			strRet = new String(src.getBytes("ISO_8859_1"), StandardCharsets.UTF_8);
		} catch (Exception e) {

		}
		return strRet;
	}

}
